package com.jwm.power.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jwm.power.util.PdfUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

//  导出PDF
@Controller
@RequestMapping("/pdf")
public class PdfController {
    //  获取用户的菜单权限
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void getUserMenuList(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> dataMap = new HashMap<>(16);

            // 设置字体
            BaseFont bfChinese = PdfUtil.setBaseFont(request);
            Font title = new Font(bfChinese, 20, Font.BOLD);
            Font cellTitle = new Font(bfChinese, 12, Font.BOLD);
            Font cellContent = new Font(bfChinese, 10, Font.NORMAL);
            // 设置版式
            Rectangle rectPageSize = new Rectangle(PageSize.A4);
            rectPageSize = rectPageSize.rotate();
            Document document = new Document(rectPageSize, 50, 50, 50, 50);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            document.open();
            // 设置标题及样式
            Paragraph pdfTitle = new Paragraph("测试的", title);
            pdfTitle.setAlignment(Element.ALIGN_CENTER);
            pdfTitle.setSpacingBefore(20);
            document.add(pdfTitle);
            int size = 20;
            int[] cellsWidth = {15, 15, 15, 15, 15, 15, 15, 15, 15, 15};
            PdfPTable table = new PdfPTable(cellsWidth.length);
            table.setWidths(cellsWidth);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setWidthPercentage(98);
            table.setSpacingBefore(10);

            Font fontChinese = new Font(bfChinese, 10, Font.NORMAL, new BaseColor(0, 0, 0));
            int rowHeight = 20;//行高

            //    BaseFont bfChinese1 = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", true);
            BaseFont bfChinese1 = BaseFont.createFont("I:\\Temp\\merge.ttf",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //设置显示中文
            Font fontChinese1 = new Font(bfChinese1,12,Font.NORMAL);

            Chunk tChunk2 = new Chunk("☑", fontChinese1);
            tChunk2.setBackground(BaseColor.CYAN, 1f, 0.5f, 1f, 1.5f); // 设置背景色
            tChunk2.setTextRise(6); // 上浮
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            document.add(tChunk2);
            int[] code = {0x2610, 0x2611, 0x2612, 0x2613, 0x2614, 0x2615, 0x2616};
            String s = new String(code, 0, 1);
            System.out.println(s);
            Image img_S = Image.getInstance("I:\\Temp\\img\\lng.png"); //图片存放路径

            PdfPCell imgCell_S = new PdfPCell();

            imgCell_S.setColspan(3);
            imgCell_S.setRowspan(2);
            imgCell_S.setImage(img_S); //图片
            imgCell_S.setFixedHeight(10);
            imgCell_S.setHorizontalAlignment(Element.ALIGN_LEFT)  ;
            imgCell_S.setVerticalAlignment(Element.ALIGN_MIDDLE) ;
            table.addCell(imgCell_S);
//            PdfUtil.AddCell(table, fontChinese1, 13, 2, 2, rowHeight, "☑", 1, 5);

            PdfUtil.AddCell(table, fontChinese1, 13, 3, 2, rowHeight, "☐", 1, 5);
            PdfUtil.AddCell(table, fontChinese1, 13, 4, 0, rowHeight, s, 1, 5);
            PdfUtil.AddCell(table, fontChinese, 13, 4, 0, rowHeight, "P4", 1, 5);
            PdfUtil.AddCell(table, fontChinese, 13, 1, 0, rowHeight, "T" , 1, 5);
            PdfUtil.AddCell(table, fontChinese, 13, 9, 0, rowHeight, "T222" , 1, 5);
            //  跨页显示
//            table.setSplitLate(false);
//            table.setSplitRows(true);
            PdfPCell cell = null;
            for (Integer i = 0; i < 10; i++) {
//                document.newPage();
               PdfUtil.AddCell(table, fontChinese, 13, 1, 0, rowHeight, "T" + i, 1, 5);


            }
            document.add(table);
            document.close();
            PdfUtil.writeFile(baos, response, "测试的.pdf");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
